package com.lf.lexy.business.bac;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.log4j.Logger;

import com.jfinal.upload.UploadFile;
import com.lf.comm.MyConstants;
import com.lf.lexy.dao.GrabUploadInfDao;
import com.lf.lexy.model.GrabUploadInf;
import com.lf.lexy.model.User;
import com.lf.lexy.webutils.CommMethod;
import com.lf.lexy.webutils.DateUtils;
import com.lf.lexy.webutils.GetWaterId;

public class FileBusiness {
	
	
	public Logger log = MyConstants.log;
	
	
	public static FileBusiness bus = new FileBusiness();
	
	
	public String fileUpload(UploadFile uploadFile , String serialId , User user){ 
		int length = 0;
		FileInputStream fis = null;
		FileOutputStream fos = null;
		String imgUrl = "";
		try {
			String uploadPath = uploadFile.getUploadPath();
			String rootPath = uploadPath.substring(0, uploadPath.indexOf("Lexy")-1)+File.separator+"upload"+File.separator+"data";
			
			//获取文件路径  rootpath+ 年 + 月 + 日
			String file_dir = CommMethod.getFileDirDate(rootPath , user) ;
			
			//获取文件名
			String fileName = "push_"+GetWaterId.getWaterId()+"."+uploadFile.getFileName().split("\\.")[1];
			String fileReallyPath = file_dir+File.separator+fileName;
			File fileReally = new File(fileReallyPath);
			fis = new FileInputStream(uploadFile.getFile());
			fos = new FileOutputStream(fileReally);
			//获取文件大小
			length = fis.available();
			// 创建缓存区域
			byte[] buffer = new byte[length];
			// 将文件中的数据写入缓存数组
			fis.read(buffer);
			// 将缓存数组中的数据输出到文件
			fos.write(buffer);
			boolean delete = uploadFile.getFile().delete();
			log.info("====> 文件上传成功，删除临时文件："+delete);
			
			GrabUploadInf grabUploadInfObj = this.getGrabUploadInfObj(file_dir, fileName , uploadFile.getFileName().split("\\.")[0]);
			GrabUploadInfDao.me.insert(grabUploadInfObj);
			imgUrl = file_dir + File.separator + fileName;
			
			log.info("====>返回文件路径："+imgUrl);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return imgUrl;
	}
	
	
	
	public GrabUploadInf getGrabUploadInfObj(String filePath, String fileName , String fileShowName) {
		GrabUploadInf gur = new GrabUploadInf();
		gur.setSerialId("_upload"+GetWaterId.getWaterId());
		gur.setFileName(fileName);
		gur.setFileShowName(fileName);
		//String[] split = fileName.split(",");
		gur.setExpandName(fileName.split("\\.")[1]);
		//String substring = filePath.substring(filePath.indexOf("Lexy") , filePath.length());
		String substring = filePath;
		substring = substring.replace("\\\\", "/");
		substring = substring.replace("\\", "/");
		substring = substring.replace("////", "/");
		substring = substring.replace("//", "/");
		gur.setUploadPath(substring.substring(substring.indexOf("upload"), substring.length()));
		String[] split = substring.split("/");
		gur.setFileType(split[split.length-1]);
		gur.setUploadTime(DateUtils.getCurrDateTime());
		gur.setUploadUser(null);
		gur.setFileShowName(fileShowName);
		return gur;
	}

}
